package com.itheima.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.CategoryDao;
import com.itheima.DishDao;
import com.itheima.DishFlavorDao;
import com.itheima.DishService;
import com.itheima.bean.Category;
import com.itheima.bean.Params;

import com.itheima.bean.Dish;
import com.itheima.bean.DishFlavor;
import com.itheima.dto.DishDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class DishServiceImpl implements DishService {
    @Autowired
    private DishDao dishDao;
    @Autowired
    private CategoryDao categoryDao;
    @Autowired
    private DishFlavorDao dishFlavorDao;


    @Override
    public List<DishDto> findUserDish(Long categoryId, int status) {
        List<DishDto>  list=new ArrayList<>();
        Category category = categoryDao.selectById(categoryId);
        LambdaQueryWrapper<Dish> dqw=new LambdaQueryWrapper<>();
        dqw.eq(Dish::getCategoryId,categoryId);
        dqw.eq(Dish::getStatus,status);
        List<Dish> dishList=  dishDao.selectList(dqw);
        for (Dish dish : dishList) {
            DishDto dishDto = new DishDto();
            dishDto.setCategoryName(category.getName());
            BeanUtils.copyProperties(dish,dishDto);
                LambdaQueryWrapper<DishFlavor> dfqw=new LambdaQueryWrapper<>();
                dfqw.eq(DishFlavor::getDishId,dish.getId());
                List<DishFlavor> dflist= dishFlavorDao.selectList(dfqw);
                dishDto.setFlavors(dflist);
            list.add(dishDto);
        }
        return list;
    }

    @Override
    public List<Dish> findDish(Long categoryID) {
        LambdaQueryWrapper<Dish> qw=new LambdaQueryWrapper<>();
        qw.eq(Dish::getCategoryId,categoryID);
        return dishDao.selectList(qw);
    }

    @Override
    public int updata(DishDto dishDto) {
        int row = dishDao.updateById(dishDto);
        int row2=0;
        if (row > 0) {
            //删除现有的口味信息，再新增口味信息
         LambdaQueryWrapper<DishFlavor> dfqw=new LambdaQueryWrapper<>();
         dfqw.eq(DishFlavor::getDishId,dishDto.getId());
            dishFlavorDao.delete(dfqw);
           if (dishDto.getFlavors()!=null&&dishDto.getFlavors().size()>0){
               for (DishFlavor flavor : dishDto.getFlavors()) {
                   flavor.setDishId(dishDto.getId());
                   row2 += dishFlavorDao.insert(flavor);
               }
           }

        }
        return (row>0&&row2==dishDto.getFlavors().size())?1:0;
    }
    @Override
    public  IPage<DishDto> findPage(Params params) {
//        1.dish分页查询
        Page<Dish> dishP = new Page<>(params.getPage(),params.getPageSize());
        LambdaQueryWrapper<Dish> qw=new LambdaQueryWrapper<>();
        qw.like(params.getName()!=null,Dish::getName,params.getName());
        IPage<Dish> dishPage = dishDao.selectPage(dishP, qw);

//        自己构建ipage<dishDto>，把dish分页查询得到的总记录数封装到ipage<dishDto>；
        IPage<DishDto> dishDtoPage = new Page<>();
        dishDtoPage.setTotal(dishPage.getTotal());
//        创建List<dishDto>集合，遍历Ipage<dish>得到dish，在遍历里创建DishDto对象,根据dish的id查找分类的名称。
        ArrayList<DishDto> dtoList = new ArrayList<>();
        for (Dish dish : dishPage.getRecords()) {

            Category category = categoryDao.selectById(dish.getCategoryId());
        /* 把dish/分类名称 封装到dto里
        把口味表也进行分页查询：根据dish的id查询
        把口味集合封装到dto
        把dao封装到list集*/
            DishDto dto = new DishDto();
            dto.setCategoryName(category.getName());
            BeanUtils.copyProperties(dish,dto);
            LambdaQueryWrapper<DishFlavor> dfqw=new LambdaQueryWrapper<>();
            dfqw.eq(DishFlavor::getDishId,dish.getId());
           List<DishFlavor> dfList=  dishFlavorDao.selectList(dfqw);
            dto.setFlavors(dfList);
            dtoList.add(dto);
        }
//        把list集合增加到ipage<dishDto>，并返回ipage<dishDto>
        dishDtoPage.setRecords(dtoList);
        return dishDtoPage;
    }

    @Override
    public int add(DishDto dishDto) {
        int row = dishDao.insert(dishDto);
        int row2=0;
     if(row>0){
         List<DishFlavor> flavorList= dishDto.getFlavors();
         if (flavorList!=null&&flavorList.size()>0){
             for (DishFlavor flavor : flavorList) {
                 Long dishId = dishDto.getId();
                 flavor.setDishId(dishId);
             /*  row2 ++;
           dishFlavorDao.insert(flavor);*/

                 row2+=dishFlavorDao.insert(flavor);
             }
         }
     }


        return (row>0&&row2==dishDto.getFlavors().size())?1:0;
    }
}
